package com.lecture.java8.http;

import cn.hutool.http.HttpUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2021/1/22 15:41
 */
public class HttpClientTest {
    public static void main2(String[] args) {
        try {
            //1.打开浏览器
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //2.声明get请求
            HttpGet httpGet = new HttpGet("http://www.huubaa.com/play/17005-0-0.html");
            //3.发送请求
            CloseableHttpResponse response = httpClient.execute(httpGet);
            //4.判断状态码
            if(response.getStatusLine().getStatusCode()==200){
                HttpEntity entity = response.getEntity();
                //使用工具类EntityUtils，从响应中取出实体表示的内容并转换成字符串
                String string = EntityUtils.toString(entity, "utf-8");
                System.out.println(string);
            }
            //5.关闭资源
            response.close();
            httpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main4(String[] args) {
        List<String> videoList = new ArrayList<>();
        int i = 1;
        for (int j = 0; j < 40; j++) {
            String s = HttpUtil.get("https://www.172mixdj.com/categories/zwmv?page=" + i);
            String regex = "href=\"(https?://[^\"]+)\"";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s);

            while (matcher.find()) {
                String hrefContent = matcher.group(1);
                if (hrefContent.contains("videos")) {
                    videoList.add(getMp4(hrefContent));
                    System.out.println("提取到的href内容： " + hrefContent);
                }
            }
            i++;
        }
        try {
            FileWriter fileWriter = new FileWriter("D:\\study\\output.txt");
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            for (String line : videoList) {
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }

            bufferedWriter.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String getMp4(String url) {
        String s = HttpUtil.get(url);
        String regex = "https?://[^\\s]+\\.mp4";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);

        String mp4 = "";
        if (matcher.find()) {
            mp4 = matcher.group();
            System.out.println("提取到的URL: " + url);
        } else {
            System.out.println("未找到匹配的URL");
        }
        return mp4;
    }

    public static void main(String[] args) {
        String url = "https://mp4.172mixdj.com/videos/20240128/65b646acd3d14.mp4";
        String outputFileName = "D:\\study\\test.mp4";

        try (InputStream inputStream = new URL(url).openStream();
             FileOutputStream fileOutputStream = new FileOutputStream(outputFileName)) {
            byte[] buffer = new byte[12288];
            int bytesRead;

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }

            System.out.println("文件已成功下载到： " + outputFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
